//
//  NoticePublishViewController.swift
//  ModernFoodWebsite
//
//  Created by apple on 16/5/30.
//  Copyright © 2016年 Lonnie. All rights reserved.
//

import UIKit

class NoticePublishViewController: BaseViewController,UITextViewDelegate {
    let scrollView = UIScrollView()
    let titleTextField:UITextField = UITextField()
    let titleHintLabel = UILabel()
    let contentTextView = UITextView()
    let contentHintLabel = UILabel()
    let imageContentView = UIView()
    let placeSelectButton = UIButton()
    let categorySelectButton = UIButton()
    let otherTagsButton = UIButton()
    let publishButton = UIButton()
    
    var publishTitle = ""
    var publishContent = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        scrollView.backgroundColor = UIColor(red: 0xea/255.0, green: 0xea/255.0, blue: 0xea/255.0, alpha: 1.0)
        addCommonStyleBackItem()
        self.navigationItem.leftBarButtonItem?.tintColor = Style.themeColor1
        
        let titleView = UIView(frame: CGRectMake(0,0,30,30))
        let headImageView = UIImageView(image: UIImage(named: "information_pic1"))
        
        titleView.addSubview(headImageView)
        headImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(titleView)
        }
        
        self.navigationItem.titleView = titleView
        headImageView.layer.cornerRadius = 15
        headImageView.layer.masksToBounds = true
        
        view.addSubview(scrollView)
        scrollView.snp_makeConstraints { (make) in
            make.edges.equalTo(self.view)
        }
        
        scrollView.addSubviews([titleTextField,contentTextView,imageContentView,placeSelectButton,categorySelectButton,otherTagsButton])
        titleTextField.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.scrollView)
            make.width.equalTo(self.scrollView).offset(-40)
            make.top.equalTo(scrollView).offset(1)
            make.height.equalTo(55)
        }
        titleTextField.backgroundColor = UIColor.whiteColor()
        titleTextField.addSubview(titleHintLabel)
        titleTextField.addTarget(self, action: #selector(NoticePublishViewController.titleChange(_:)), forControlEvents: .AllEditingEvents)
        titleHintLabel.snp_makeConstraints { (make) in
            make.centerY.equalTo(titleTextField)
            make.leading.equalTo(titleTextField).offset(18)
        }
        let attr1 = NSAttributedString(string: "标题", attributes: [NSFontAttributeName:UIFont.systemFontOfSize(18),NSForegroundColorAttributeName:Style.textColor3])
        let attr2 = NSAttributedString(string: "(20字内)", attributes: [NSFontAttributeName:UIFont.systemFontOfSize(12),NSForegroundColorAttributeName:Style.textColor3])
        let mutable = NSMutableAttributedString()
        mutable.appendAttributedString(attr1)
        mutable.appendAttributedString(attr2)
        titleHintLabel.attributedText = mutable
        
        contentHintLabel.attributedText = NSAttributedString(string: "请输入公告内容", attributes: [NSFontAttributeName:UIFont.systemFontOfSize(14),NSForegroundColorAttributeName:Style.textColor3])
        
        contentTextView.backgroundColor = UIColor.whiteColor()
        contentTextView.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(self.titleTextField)
            make.top.equalTo(titleTextField.snp_bottom).offset(1)
            make.height.equalTo(500)
        }
        
        
        contentTextView.addSubview(contentHintLabel)
        contentTextView.delegate = self
        contentHintLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(contentTextView).offset(18)
            make.top.equalTo(contentTextView).offset(18)
        }
        contentHintLabel.attributedText = NSAttributedString(string: "请输入公告内容", attributes: [NSFontAttributeName:UIFont.systemFontOfSize(14),NSForegroundColorAttributeName:Style.textColor3])
        imageContentView.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(scrollView)
            make.top.equalTo(contentTextView.snp_bottom).offset(1)
            make.height.equalTo(130)
        }
        
        placeSelectButton.backgroundColor = UIColor(red: 0xea/255.0, green: 0xea/255.0, blue: 0xea/255.0, alpha: 1.0)
        placeSelectButton.setTitleColor(UIColor(red: 0x66/255.0, green: 0x66/255.0, blue: 0x66/255.0, alpha: 1.0), forState: .Normal)
        placeSelectButton.setTitle("选择地点", forState: .Normal)
        placeSelectButton.snp_makeConstraints { (make) in
            make.leading.equalTo(scrollView).offset(20)
            make.top.equalTo(contentTextView.snp_bottom)
            make.width.equalTo(scrollView).multipliedBy(0.5).offset(-30)
            make.height.equalTo(42)
        }
        
        categorySelectButton.backgroundColor = UIColor(red: 0xea/255.0, green: 0xea/255.0, blue: 0xea/255.0, alpha: 1.0)
        categorySelectButton.setTitleColor(UIColor(red: 0x66/255.0, green: 0x66/255.0, blue: 0x66/255.0, alpha: 1.0), forState: .Normal)
        categorySelectButton.setTitle("选择分类", forState: .Normal)
        categorySelectButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(contentTextView)
             make.centerY.width.height.equalTo(placeSelectButton)
        }
        otherTagsButton.backgroundColor = UIColor(red: 0xea/255.0, green: 0xea/255.0, blue: 0xea/255.0, alpha: 1.0)
        otherTagsButton.setTitleColor(UIColor(red: 0x66/255.0, green: 0x66/255.0, blue: 0x66/255.0, alpha: 1.0), forState: .Normal)
        otherTagsButton.setTitle("选择添加其他标签", forState: .Normal)
        otherTagsButton.snp_makeConstraints { (make) in
            make.centerX.equalTo(scrollView)
            make.width.equalTo(scrollView).offset(-40)
            make.height.equalTo(42)
            make.top.equalTo(categorySelectButton.snp_bottom).offset(20)
            make.bottom.equalTo(scrollView.snp_bottom).offset(-60)
        }
        
        view.addSubview(publishButton)
        publishButton.backgroundColor = Style.themeColor1
        publishButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        publishButton.setTitle("发布", forState: .Normal)
        publishButton.snp_makeConstraints { (make) in
            make.leading.trailing.bottom.equalTo(view)
            make.height.equalTo(54)
        }
        publishButton.addTarget(self, action: #selector(NoticePublishViewController.publish), forControlEvents: .TouchUpInside)
        view.addExitKeyBoardGestureRecognizer()
        
    }
    
    func publish() {
        _ = UIAlertController(message:"发布消息成功!",context:self)
  
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2), dispatch_get_main_queue()) {
            self.navigationController?.popViewControllerAnimated(true)
        }
    }
    
    func titleChange(t:UITextField)  {
        publishTitle = t.text!
        self.titleHintLabel.alpha = (publishTitle.characters.count > 0) ? 0 : 1
    }

    func textViewDidChange(textView: UITextView) {
        publishContent = textView.text
        self.contentHintLabel.alpha = (publishContent.characters.count > 0)  ? 0 : 1
    }
}
